<?php

namespace App\Jobs;

use App\Services\WxCard\WxCardMemberCard;
use App\Services\WxCard\WxCardService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

/**
 * 微信卡券-会员数据更新
 * @package App\Jobs
 */
class WxCardUpset implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $appid;
    protected $eventData;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($appid, $eventData)
    {
        $this->appid = $appid;
        $this->eventData = $eventData;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $wxCardService = new WxCardService($this->appid);

        $cardId = isset($this->eventData['CardId']) ? $this->eventData['CardId'] :'';

        $userCode = isset($this->eventData['UserCardCode']) ? $this->eventData['UserCardCode'] :'';
        $cardData = $wxCardService->getCardByCardId($cardId);
        if (!empty($cardData['card_type'])){
            $wxCardService->createCardObj($cardData['card_type'])->upsetUserinfo($cardId, $userCode);
        }
    }
}
